<?php
/**
 * Created by Chukintang on 2018/5/20 18:07.
 */

namespace app\common\validate;

use app\common\model\Cate as CateModel;

class ArticleValidate extends CommonValidate
{

    protected $rule = [
        'cate_id'=>'require|checkExistCate',
        'title'=>'require',
        'cover_pic'=> 'require',
        'content'=> 'require',
        'add_time'=> 'require',
        'edit_time'=> 'require',
        'article_sort'=> 'require|integer|between:0,127',
    ];

    protected $message = [
        'cate_id.require'=>'请选择分类',
        'cate_id.checkExistCate'=>'分类不存在',
        'title'=> '请输入标题',
        'cover_pic'=> '请上传封面图片',
        'content'=>  '请输入内容',
        'add_time'=>  '创建时间异常',
        'edit_time'=>  '编辑时间异常',
        'article_sort'=>  '请输入0-127的排序数字',
    ];
    protected $scene = [
        'add' => ['cate_id', 'title', 'cover_pic','content', 'article_sort','add_time'],
        'edit' => ['cate_id', 'title', 'cover_pic','content', 'article_sort','edit_time'],
    ];


    /**
     * 检测分类是否存在
     * @desc 检测分类是否存在
     */
    protected function checkExistCate($value, $rule, $data, $field, $title)
    {
        $cateModel = new CateModel();
        if (!$cateModel->where(['id' => $value])->find()) {
            return '分类不存在';
        }
        return true;
    }
}

